<?php
namespace Modules\Asset\Http\Controllers\Client\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use \Modules\Asset\Entities\BankCard;

use Validator;
use ThrowException;
use DB;
use Auth;

class BankCardController extends Controller
{
    // 获取开奖列表
    public function index(Request $req)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $user = Auth::requireLoginUser();

        // $class = @config('activity.map')[$activityType] ?: ThrowException::Conflict('无效的活动类型');

        $options = $req->all();
        $options['where']['user_id'] = $user->id;
        // $options['order_by'] = ['list_weight' => 'desc'];

        $list = BankCard::getList($options);

        return $list->generateListResponse();
    }

    public function store(Request $req)
    {
        $rule = [
            'bank_name' => ['required'],
            'bank_account_name' => ['required'],
            'bank_account_number' => ['required'],
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $user = Auth::requireLoginUser();

        // 判断是否已存在
        if (BankCard::where('user_id', $user->id)->where('bank_account_number', $req->bank_account_number)->first()) {
            ThrowException::Conflict('此银行卡号已创建过~');
        }

        if (strlen($req->bank_account_number) < 13) {
            ThrowException::Conflict('银行卡号至少13位~');
        }
        if (strlen($req->bank_account_number) > 19) {
            ThrowException::Conflict('银行卡号最长19位~');
        }

        $info = $req->all();

        $info['user_id'] = $user->id;

        $item = BankCard::create($info);

        return [
            'id' => $item->id,
            'uuid' => $item->uuid
        ];
    }

    // public function show(Request $req, $uuid)
    // {
    //     $rule = [
    //     ];
    //     Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

    //     $user = Auth::requireLoginUser();

    //     $item = BankCard::getEntity([
    //         'uuid' => $uuid
    //     ]);

    //     $info = $item->getInfo();

    //     return [
    //         'info' => $info
    //     ];
    // }

    public function update(Request $req, $uuid)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $user = Auth::requireLoginUser();

        $item = BankCard::getEntity([
            'uuid' => $uuid,
            'user_id' => $user->id
        ]);

        if ($req->type === 'update') {
            $info = $req->input('attributes');
            $item->update($info);
        }

        return [];
    }

    public function destroy(Request $req, $uuid)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $user = Auth::requireLoginUser();

        $item = BankCard::getEntity([
            'user_id' => $user->id,
            'uuid' => $uuid
        ]);

        $item->delete();

        return [];
    }
}
